import { useSearchParam } from "shared/hooks";
import YearIcon from "@/assets/icons/year.svg?jsx";
import { useQuery } from "@tanstack/react-query";
import { request } from "shared/requests";
import { defaultUri } from "@/lib/constants";
import { useMemo } from "react";

export default function Footer() {
  const uri = useSearchParam("uri", defaultUri);

  const { data: info, isSuccess } = useQuery({
    queryKey: ["person-info"],
    queryFn: () =>
      request<{ data: string; result: string }>(
        "/mrgf/service/person/nianpusum",
        { params: { uri }, headers: { accept: "application/json" } },
      ),
    staleTime: Infinity,
  });

  const parsed = useMemo<{
    bookName: string;
    description: string;
    personName: string;
  }>(() => {
    if (!isSuccess || info.result !== "0") {
      return { bookName: "", description: "-", personName: "-" };
    }
    return JSON.parse(info.data);
  }, [isSuccess, info]);

  return (
    <footer className="bg-[#EBEBEB] py-10 text-center">
      <div className="inline-flex items-center text-[#1E1E1E]">
        <YearIcon />
        <span className="ml-2 font-bold">
          {parsed.personName}年谱数据文献来源：
          {parsed.bookName ? `《${parsed.bookName}》` : "-"}
        </span>
      </div>
      <div className="text-sm text-[#757575]">
        Copyright 2021-2023 Shanghai Library All Rights Reserved.
        <br />
        上海图书馆(上海科学技术情报研究所) 版权所有
      </div>
    </footer>
  );
}
